package main

import (
	"context"
	"fmt"

	"github.com/influxdata/influxdb-client-go/v2"
)

func main() {
	// Create a new client using an InfluxDB server base URL and an authentication token
	client := influxdb2.NewClient("http://localhost:8087", "w-XXleSA25LOI0hEhb60vx5aKEOujMAtkCr0EPwAKMYdOcgRm7qo_18mWUjlUXSaab3UUQuwh-rSY9DhxwuZmQ==")
	// Get query client
	queryAPI := client.QueryAPI("xyf")
	// get QueryTableResult
	result, err := queryAPI.Query(context.Background(), `from(bucket: "test-bucket")
  |> range(start: -3h)
  |> filter(fn: (r) => r["_measurement"] == "client_error")
  |> aggregateWindow(every: 1h, fn: count)`)
	if err == nil {
		// Iterate over query response
		for result.Next() {
			// Notice when group key has changed
			if result.TableChanged() {
				// fmt.Printf("table: %s\n", result.TableMetadata().String())
			}
			// Access data
			// fmt.Printf("value: %v\n", result.Record().Value())
			fmt.Println(result.Record().Values())
		}
		// check for an error
		if result.Err() != nil {
			fmt.Printf("query parsing error: %s\n", result.Err().Error())
		}
	} else {
		panic(err)
	}
	// Ensures background processes finishes
	client.Close()
}
